package utility;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;


public class NumberRestrictionDocument extends PlainDocument {

    private final int limit;

    public NumberRestrictionDocument(int limit) {
        this.limit = limit;
    }

    
    @Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        if (str == null) {
            return;
        }
        if ((getLength() + str.length()) <= limit && str.matches("\\d+")) {
            super.insertString(offs, str, a);
        }
    }
}